pub mod oc_cluster {
    tonic::include_proto!("oc_cluster");
}

use tokio::io::{AsyncReadExt, AsyncWriteExt};

// 长度前缀帧：u32 BE + bytes
pub async fn write_frame<W: AsyncWriteExt + Unpin>(writer: &mut W, bytes: &[u8]) -> std::io::Result<()> {
    let len = bytes.len() as u32;
    writer.write_all(&len.to_be_bytes()).await?;
    writer.write_all(bytes).await?;
    writer.flush().await
}

pub async fn read_frame<R: AsyncReadExt + Unpin>(reader: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
    let mut len_bytes = [0u8; 4];
    reader.read_exact(&mut len_bytes).await?;
    let len = u32::from_be_bytes(len_bytes) as usize;
    buf.resize(len, 0);
    reader.read_exact(buf).await?;
    Ok(len)
}


